﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComputerArchitecture.Numbers
{
    public sealed class Bit : ICloneable, IEquatable<Bit>
    {
        #region Fields

        private bool _value;

        #endregion

        #region Properties

        public bool IsSet
        {
            get { return _value; }
        }

        public static Bit Zero
        {
            get { return new Bit(false); }
        }

        public static Bit One
        {
            get { return new Bit(true); }
        }

        #endregion

        #region Constructors

        private Bit(bool value)
        {
            _value = value;
        }

        #endregion

        #region Methods

        public void Set()
        {
            if (!_value)
            {
                _value = true;
            }
        }

        public void Reset()
        {
            if (_value)
            {
                _value = false;
            }
        }

        public void SetAs(Bit other)
        {
            if (ReferenceEquals(other, null))
            {
                throw new ArgumentNullException("other");
            }
            _value = other._value;
        }

        public object Clone()
        {
            return new Bit(_value);
        }

        public bool Equals(Bit other)
        {
            if (ReferenceEquals(other, null))
            {
                return ReferenceEquals(this, null);
            }
            return _value.Equals(other._value);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(obj, null))
            {
                return false;
            }
            Bit other = obj as Bit;
            return !ReferenceEquals(other, null) && this.Equals(other);
        }

        public override int GetHashCode()
        {
            return _value.GetHashCode();
        }

        public override string ToString()
        {
            return _value ? "1" : "0";
        }

        #region Bit Manipulation

        public void And(Bit other)
        {
            _value &= other._value;
        }

        public void Or(Bit other)
        {
            _value |= other._value;
        }

        public void Not()
        {
            _value = !_value;
        }

        public void Xor(Bit other)
        {
            _value ^= other._value;
        }

        #endregion

        #region Operators

        public static implicit operator bool(Bit other)
        {
            return other._value;
        }

        public static explicit operator Bit(bool value)
        {
            return new Bit(value);
        }

        public static Bit And(Bit first, Bit second)
        {
            return new Bit(first._value && second._value);
        }

        public static Bit operator &(Bit first, Bit second)
        {
            return And(first, second);
        }

        public static Bit Or(Bit first, Bit second)
        {
            return new Bit(first._value || second._value);
        }

        public static Bit operator |(Bit first, Bit second)
        {
            return Or(first, second);
        }

        public static Bit Not(Bit value)
        {
            return new Bit(!value._value);
        }

        public static Bit operator !(Bit value)
        {
            return Not(value);
        }

        public static Bit Xor(Bit first, Bit second)
        {
            return new Bit(first._value ^ second._value);
        }

        public static Bit operator ^(Bit first, Bit second)
        {
            return Xor(first, second);
        }

        #endregion

        #endregion
    }
}
